package com.wfyvv.resteasy.common.filter;

import com.google.common.collect.Lists;
import com.wfyvv.resteasy.common.util.UserUtils;
import org.jboss.resteasy.annotations.interception.ServerInterceptor;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Cookie;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
import java.util.List;

@Provider
@ServerInterceptor
public class LoginFilter implements ContainerRequestFilter {

    private final List<String> exceptUrls = Lists.newArrayList("/uc/login","/test");

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {
        if (exceptUrls.contains(requestContext.getUriInfo().getPath())) {
            return;
        }
        Cookie cookie = requestContext.getCookies().values().stream().filter(x -> "token".equals(x.getName())).findFirst().orElse(null);
        if (cookie == null || UserUtils.isLogin(cookie.getValue()) == null) {
            throw new RuntimeException("未登陆");
        }
    }
}
